<template>
	<div class="box">
		<van-nav-bar
		  title="登录中心"
		  left-text="返回"
		  left-arrow
		  @click-left="onClickLeft"
		/>
		<van-form @submit="onSubmit">
		  <van-field
		    v-model="username"
		    name="username"
		    label="用户名"
		    placeholder="用户名"
		    :rules="[{ required: true, message: '请填写用户名' }]"
		  />
		  <van-field
		    v-model="password"
		    type="password"
		    name="password"
		    label="密码"
		    placeholder="密码"
		    :rules="[{ required: true, message: '请填写密码' }]"
		  />
		  {{mess}}
		  {{mass}}
		  <div style="margin: 16px;">
		    <van-button round block type="info" native-type="submit" >登录</van-button>
		  </div>
		  <div style="margin: 16px;">
		    <van-button round block type="primary" to="/register">注册</van-button>
		  </div>
		</van-form>
	</div>
</template>

<script>
	// 导航栏
	import { Toast } from 'vant';
	export default{
		name:'box',
		data() {
		    return {
		      username: '',
		      password: '',
			  mess:'',
			  mass:'',
			  nickname:''
		    };
		  },
		// 方法
		methods:{
			onClickLeft(){
				console.log(1)
			},
			onSubmit(values) {
				  this.$axios({
				    method: 'post',
				    url: '/api/login',
				    data: {
				      username: values.username,
				      password: values.password,
					  nickname:values.username
				    }
				  }).then(res=>{
					  if(res.data.err === 0){
						  this.mess = res.data.msg
						  console.log(res.data.data)
						  console.log(res)
						  let user = JSON.stringify(res.data)
						  window.localStorage.setItem("user",user)
						  this.$router.push(`/user`)
					  }else{
						  this.mass = res.data.msg
					  }
				  })
			},
			
		}
	}
</script>

<style>
</style>
